package com.gexiaobao.pigeon.ui.fragment.mine.message

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.databinding.FragmentNoticeDetailBinding
import com.gexiaobao.pigeon.ui.home.HomeViewModel
import me.hgj.jetpackmvvm.ext.nav
import org.jsoup.Jsoup

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:37
 * @Describe    : 公告详情
 */
class FragmentNoticeDetail : BaseFragment<HomeViewModel, FragmentNoticeDetailBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("公告详情") {
            nav().navigateUp()
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        val id = requireArguments().getInt("id")
        showLoading(getString(R.string.loading))
        mViewModel.getMessageResult(id.toString())
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.singleMsgResult.observe(viewLifecycleOwner) {
            if (it != null) {
                mDatabind.data = it
                val htmlContent = it.body
                val doc = Jsoup.parse(htmlContent)
                val head = doc.getElementsByTag("head")
                val style = doc.createElement("style")
                style.attr("type", "text/css")
                style.append("img{width:100%}")
                head[0].appendChild(style)
                mDatabind.orgDetailNoticeDes.loadDataWithBaseURL(
                    "about:blank",
                    doc.toString(), "text/html", "utf-8", null
                )
            }
        }
    }
}